import { Component, OnInit, AfterViewInit, EventEmitter, Output, Input } from '@angular/core';


@Component({
  selector: 'el-collapse',
  templateUrl: './collapse.component.html'
})
export class CollapseComponent implements AfterViewInit {
  @Input() modelValue: Array<any> = [];
  @Input() subscriber: any = [];
  @Input() accordion: boolean = false;
  @Input() set model(val: any) {
    this.modelValue = val;
    this.updateSubscriber();
  };
  @Output('change') modelChange: EventEmitter<any[]> = new EventEmitter();
  constructor() { }


  updateModel(value: any): void {
    var /** @type {?} */ index = this.modelValue.findIndex(function (val) { return val === value; });
    if (index < 0) {
      this.accordion && (this.model = []);
      this.modelValue.push(value);
      this.updateSubscriber();
      return this.modelChange.emit(this.modelValue);
    }
    if (this.accordion) {
      this.model = [];
    }
    else {
      this.modelValue.splice(index, 1);
    }
    this.updateSubscriber();
    this.modelChange.emit(this.modelValue);
  };

  ngAfterViewInit(): void {
    var _this = this;
    var /** @type {?} */ timer = window.setTimeout(function () {
      _this.updateSubscriber();
      window.clearTimeout(timer);
    }, 0);
  };

  updateSubscriber() {
    this.subscriber.forEach(function (handle: any) { return handle(); });
  }
}
